using System;
using System.Reflection;
using System.Runtime.Serialization;

namespace SharpCore.NullableTypes
{
	/// <summary>
	/// Wraps the <see cref="System.Char"/> value type in an object so that references can be null.
	/// </summary>
	[Serializable]
	public sealed class NullableChar : INullable
	{
		/// <summary>
		/// The <see cref="System.Char"/> value this instance represents.
		/// </summary>
		private char charValue;

		/// <summary>
		/// Initializes a new instance of the NullableChar class.
		/// </summary>
		public NullableChar()
		{
			this.charValue = '\u0000';
		}

		/// <summary>
		/// Initializes a new instance of the NullableChar class with a <see cref="System.Char"/> value.
		/// </summary>
		/// <param name="charValue">The <see cref="System.Char"/> that this instance represents.</param>
		public NullableChar(char charValue)
		{
			this.charValue = charValue;
		}

		/// <summary>
		/// Initializes a new instance of the NullableChar class with a <see cref="System.String"/> that represents a <see cref="System.Char"/>.
		/// </summary>
		/// <param name="charValue">The string representation of a <see cref="System.Char"/> value.</param>
		public NullableChar(string charValue)
		{
			this.charValue = Char.Parse(charValue);
		}

		/// <summary>
		/// Creates a NullableChar that is serializable with the specified SerializationInfo and StreamingContext.
		/// </summary>
		/// <param name="info">A SerializationInfo object containing the information required to serialize the NullableChar.</param>
		/// <param name="context">A StreamingContext object containing the source and destination of the serialized stream associated with the NullableChar.</param>
		private NullableChar(SerializationInfo info, StreamingContext context)
		{
			this.charValue = (char) info.GetChar("Value");
		}

		/// <summary>
		/// Returns the data needed to serialize the NullableChar.
		/// </summary>
		/// <param name="info">A SerializationInfo object containing the information required to serialize the NullableChar.</param>
		/// <param name="context">A StreamingContext object containing the source and destination of the serialized stream associated with the NullableChar.</param>
		void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
		{
			info.AddValue("Value", charValue);
		}

		/// <summary>
		/// Gets or Sets the <see cref="System.Char"/> value this instance represents.
		/// </summary>
		public char CharValue
		{
			get { return charValue; }
			set { charValue = value; }
		}

		/// <summary>
		/// Converts a <see cref="System.Char"/> to a NullableChar.
		/// </summary>
		/// <param name="charValue">The <see cref="System.Char"/> to be converted.</param>
		/// <returns>A NullableChar instance that wraps the <see cref="System.Char"/> value.</returns>
		public static implicit operator NullableChar(char charValue)
		{
			return new NullableChar(charValue);
		}

		/// <summary>
		/// Checks two NullableChar instances for equality.
		/// </summary>
		/// <param name="a">A NullableChar instance to test for equality.</param>
		/// <param name="b">A NullableChar instance to test for equality.</param>
		/// <returns><b>true</b> if both instances are the same; otherwise <b>false</b>.</returns>
		public static bool operator ==(NullableChar a, NullableChar b)
		{
			if (a == null && b == null)
			{
				return true;
			}
			else if (a == null || b == null)
			{
				return false;
			}
			else
			{
				return (a.CharValue == b.CharValue);
			}
		}

		/// <summary>
		/// Checks two NullableChar instances for inequality.
		/// </summary>
		/// <param name="a">A NullableChar instance to test for inequality.</param>
		/// <param name="b">A NullableChar instance to test for inequality.</param>
		/// <returns><b>true</b> if each instance is different; otherwise <b>false</b>.</returns>
		public static bool operator !=(NullableChar a, NullableChar b)
		{
			if (a == null && b == null)
			{
				return false;
			}
			else if (a == null || b == null)
			{
				return true;
			}
			else
			{
				return (a.CharValue != b.CharValue);
			}
		}

		/// <summary>
		/// Compares this instance to a specified object and returns an indication of their relative values.
		/// </summary>
		/// <param name="obj">An object to compare to this instance, or a null reference.</param>
		/// <returns>A signed integer that indicates the relative order of this instance and obj.</returns>
		public int CompareTo(object obj)
		{
			if (obj == null)
			{
				return 1;
			}
			else if (obj is NullableChar)
			{
				NullableChar nullableChar = obj as NullableChar;
				return charValue.CompareTo(nullableChar.CharValue);
			}
			else if (obj is char)
			{
				return charValue.CompareTo(obj);
			}
			else
			{
				throw new ArgumentException("The specified object was neither a char nor a NullableChar instance.");
			}
		}

		/// <summary>
		/// Returns a value indicating whether this instance is equal to a specified object.
		/// </summary>
		/// <param name="obj">An object to compare to this instance.</param>
		/// <returns><b>true</b> if obj is a NullableChar and has the same value as this instance; otherwise, <b>false</b>.</returns>
		public override bool Equals(object obj)
		{
			return charValue.Equals(obj);
		}

		/// <summary>
		/// Returns the hash code for this instance.
		/// </summary>
		/// <returns>A hash code for the current NullableChar.</returns>
		public override int GetHashCode()
		{
			return charValue.GetHashCode();
		}

		/// <summary>
		/// Converts the value of this instance to its equivalent string representation.
		/// </summary>
		/// <returns>The string representation of the value of this instance.</returns>
		public override string ToString()
		{
			return charValue.ToString();
		}
	}
}
